package com.example.zhihu.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zhihu.entity.HotInfo;
import com.example.zhihu.entity.WeekHot;
import com.example.zhihu.mapper.WeekHotMapper;
import com.example.zhihu.service.IHotInfoService;
import com.example.zhihu.service.IWeekHotService;
import com.example.zhihu.vo.TopicsDataVo;
import com.example.zhihu.vo.WeekHotVo;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.management.Query;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-05-05
 */
@Service
public class WeekHotServiceImpl extends ServiceImpl<WeekHotMapper, WeekHot> implements IWeekHotService {
  @Resource
  IHotInfoService iHotInfoService;

  @Resource
  WeekHotMapper weekHotMapper;


  @Override
  public Integer getTopicAnswer(String topic) {
    List<WeekHot> weekHotList = this.list();
    Optional<Integer> reduce = weekHotList.stream().filter(weekHot -> {
      String topics = weekHot.getTopics();
      if (StrUtil.isEmpty(topics)) {
        return false;
      }
      List<String> topicList = Arrays.stream(topics.split(",")).collect(Collectors.toList());
      return topicList.contains(topic);
    }).map(WeekHot::getNewAnswerNum).reduce(Integer::sum);

    return reduce.orElse(0);
  }

  public List<WeekHotVo> getAdd(List<HotInfo> hotInfoList) {

    return hotInfoList.parallelStream().map(hotInfo -> {
      WeekHotVo weekHotVo = new WeekHotVo();

      List<String> collect = Arrays.stream(hotInfo.getKeyword().split(",")).collect(Collectors.toList());
      // 定义新增回答的总数
      int sum = 0;
      for (String s : collect) {
        Integer i = this.getTopicAnswer(s);
        sum += i;
      }
      weekHotVo.setAnswerCount(sum);
      weekHotVo.setTopics(collect);
      weekHotVo.setId(hotInfo.getInfoId());
      weekHotVo.setTitle(hotInfo.getTitle());
      return weekHotVo;
    }).sorted((o1, o2) -> o2.getAnswerCount().compareTo(o1.getAnswerCount())).collect(Collectors.toList());
  }

  @Override
  public TopicsDataVo getTopicsData(String id) {
    HotInfo hotInfo = iHotInfoService.getById(id);
    List<String> topicList = Arrays.stream(hotInfo.getKeyword().split(",")).collect(Collectors.toList());

    TopicsDataVo topicsDataVo = new TopicsDataVo();
    topicsDataVo.setInfoId(id);
    topicsDataVo.setTitle(hotInfo.getTitle());
    topicsDataVo.setTopics(topicList);

    // 检索主题在小时榜中出现的数据
    for (String topic : topicList) {
      List<WeekHot> weekHotList = getWeekHotByTopic(topic);
      for (WeekHot weekHot : weekHotList) {

      }
    }
    return topicsDataVo;
  }

  /**
   * 获取包含该主题的话题
   * @param topic
   * @return
   */
  private List<WeekHot> getWeekHotByTopic(String topic) {
    QueryWrapper<WeekHot> queryWrapper = new QueryWrapper<>();
    queryWrapper.like("topics", "%" + topic + "%");
    List<WeekHot> weekHotList = weekHotMapper.selectList(queryWrapper);
    weekHotList = weekHotList.stream().filter(weekHot -> {
      String topics = weekHot.getTopics();
      List<String> collect = Arrays.stream(topics.split(",")).collect(Collectors.toList());
      return collect.contains(topic);
    }).collect(Collectors.toList());
    return weekHotList;
  }

}
